<?php



namespace App\Http\Controllers\Backend1;

use App\Http\Requests\Backend\CourseRequest;
use App\Services\Base\Interfaces\ConfigServiceInterface;
use App\Services\Business\Interfaces\CourseServiceInterface;
use App\Services\Business\Models\Course;
use App\Services\Business\Models\Category;
use App\Services\Business\Models\CourseTag;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class ArticleController extends BaseController
{
    public function all(Request $request)
    {
        $corpId  = $request->header("corpid", 0);
        $courses = Course::corpId($corpId)->select(['id', 'title'])->get();
        return $this->successData(['data' => $courses]);
    }

    public function index(Request $request)
    {
        $id       = $request->input('id');
        $keywords = $request->input('keywords', '');
        $cid      = $request->input('cid');
        $corpId   = $request->header("corpid", 0);
        // 排序
        $sort  = $request->input('sort', 'created_at');
        $order = $request->input('order', 'desc');

        $courses = Course::corpId($corpId)
            ->select([
                'id', 'user_id', 'title', 'slug', 'thumb', 'charge', 'short_description',
                 'is_show', 'category_id', 'is_recommend', 'user_count', 'is_free',
                'created_at', 'updated_at',
            ])
            ->with(['category:id,name'])
            ->withCount(['videos', 'chapters', 'comments'])
            ->when($id, function ($query) use ($id) {
                $query->where('id', $id);
            })
            ->when($keywords, function ($query) use ($keywords) {
                return $query->where('title', 'like', '%' . $keywords . '%');
            })
            ->when($cid, function ($query) use ($cid) {
                return $query->whereCategoryId($cid);
            })
            ->where("course_type", 2)
            ->orderBy($sort, $order)
            ->paginate($request->input('size', 10));

        $categories = Category::query()->select(['id', 'name'])->orderBy('sort')->get();

        return $this->successData(compact('courses', 'categories'));
    }


    public function store(CourseRequest $request, Course $course)
    {
        $course->course_type = 2;
        $course->fill($request->filldata())->save();
        $tags          = (array)$request->input('tags');
        $tagCategoryId = (int)$request->input('tag_category_id');
        if (!empty($tags)) {
            foreach ($tags as $tag) {
                CourseTag::create([
                    "course_id"       => $course->id,
                    "video_tags_id"   => $tag,
                    "tag_category_id" => $tagCategoryId
                ]);
            }
        }
        return $this->success();
    }


    public function edit($id)
    {
        $course    = Course::query()->where('id', $id)->firstOrFail();
        $videoTags = CourseTag::query()->where('course_id', $id)->get();
        $tags      = [];
        if (!empty($videoTags)) {
            foreach ($videoTags as $v) {
                $tags[] = $v['video_tags_id'];
            }
        }
        $course->tags = $tags;
        return $this->successData($course);
    }

    public function update(CourseRequest $request, $id)
    {
        $data   = $request->filldata();
        $course = Course::query()->where('id', $id)->firstOrFail();
        $course->fill($data)->save();

        return $this->success();
    }

    public function destroy($id, ConfigServiceInterface $configService, CourseServiceInterface $courseService)
    {

        $course = Course::query()->where('id', $id)->firstOrFail();
        try {
            $courseService->deleteVideo($course, $configService);
        } catch (\Exception $exception) {
            Log::error($exception);
        }
        $course->delete();
        return $this->success();
    }


}
